package messages;

import java.sql.ResultSet;

import controllers.DBConnection;
import ocsf.server.ConnectionToClient;


public class ReplayToClientMessage {
	
	ConnectionToClient client;
	String[] arr;
	double refund;
	static String mail;
	static String msgText;
	
	public ReplayToClientMessage(){
		
	}
	
	public ReplayToClientMessage(String[] strMsg, ConnectionToClient client){
		this.arr=strMsg;
		this.client=client;
		ReplayToClientMessage.mail=arr[4];
		ReplayToClientMessage.msgText=arr[2];
	}
	
	public void activateReplay(){
		
		if(arr[1].equals("")){
			refund=0;
			
		}
		else{
			refund=Double.valueOf(arr[1]);
		}
		DBConnection conn=new DBConnection();
		try{
		
		java.sql.PreparedStatement pr= conn.getDBConnection().prepareStatement("SELECT bill FROM sgi_system.customer WHERE customerID =? ");
		pr.setString(1, arr[3]);
		ResultSet rs=pr.executeQuery();
		while (rs.next())
		refund=rs.getDouble("bill")-refund;
		java.sql.PreparedStatement pr1 = conn.getDBConnection().prepareStatement( "Update sgi_system.customer SET bill=? WHERE customerID=? ");
        pr1.setDouble(1,refund);
        pr1.setString(2,arr[3]);
        rs.close();
        pr.close();
		pr1.executeUpdate();	
		pr1.close();
		client.sendToClient("complaintHandled");
		java.sql.PreparedStatement pr4 = conn.getDBConnection().prepareStatement( "Delete FROM sgi_system.complaints  WHERE complaintID=? ");
		int temp=Integer.valueOf(arr[5]);
		pr4.setInt(1,temp);
		pr4.executeUpdate();
		pr4.close();
		}
		catch(Exception e1){e1.printStackTrace();}
		
		
	}
	

	public String getEmail(){
		
		return ReplayToClientMessage.mail;
	}
	public String getMessageFromWorker(){
		
		return msgText;
	}
}
